#include "PhysicalOperatorsGenerator.h"
#include "SearchEngine.h"

PhysicalOperatorsGenerator::PhysicalOperatorsGenerator(int property, int priority)
	: Generator("PhysicalOperatorsGenerator", property, priority)
{	
}

PhysicalOperatorsGenerator::~PhysicalOperatorsGenerator()
{	
}

bool PhysicalOperatorsGenerator::generate(Query *q, QueryStorage *qs, int sp, int *queryNumber)
{
	query = q;
	queryStorage = qs;
	startpos = sp;
	number = queryNumber;

	opsList.clear();
	query->getOpsList(opsList);

	if (startpos >= opsList.size())
	{
		return false;
	}

	generate(opsList.begin() + startpos);
	
	return true;
}

void PhysicalOperatorsGenerator::generate(std::vector <OperatorNode *> :: iterator begin)
{
	std::vector<PhysicalOperator *> list;
	findPhysicalOperatorbyName((*begin)->op->getName(), list);

	if (list.size() == 1)
	{
		return;
	}

	for (std::vector<PhysicalOperator *>::iterator i = list.begin(); i != list.end(); i++)
	{
		if ((*begin)->physOp == *i)
		{
			continue;
		}
		
		OperatorNode *node = new OperatorNode(**begin);
		node->physOp = *i;
		OperatorNode *t = *begin;
		*begin = node;
		Query *q = createQuery(opsList.begin());
		*begin = t;

		queryStorage->addQuery(new QueryInfo(q, ++(*number)));
	}
}
